<template>
  <a-card title="风采展示" :bordered="false" class="br-10">
    <template #extra>
      <a-button type="primary" @click="handleAdd">新增</a-button>
      <a-button style="margin-left: 10px;" @click="handleOk">刷新</a-button>
    </template>
    <s-table size="default" ref="table" rowKey="id" :rowSelection="null" :columns="columns" :data="loadData">
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="image" slot-scope="text">
        <ImgOrVideo :item="{url: text}" preview :width="80" :height="80" />
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" ghost size="small" class="_mr-5" @click="handleSmallEdit(record)">
          编辑
        </a-button>
        <a-button type="primary" ghost size="small" class="_mr-5" @click="delByIds(record)">
          删除
        </a-button>
      </span>
    </s-table>
    <elegant-form ref="elegantForm" @add="handleOk" @upd="handleReload" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { elegantlList, elegantlDel } from '@/api/system/system'
import elegantForm from './elegantForm'
import ImgOrVideo from '@/components/ImgOrVideo';

export default {
  name: 'MemberType',
  components: {
    STable,
    elegantForm,
    ImgOrVideo
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var params = {
          current: parameter.pageNum,
          size: parameter.pageSize
        }
        return elegantlList(Object.assign(params, this.queryParam)).then(res => {
          this.pageNum = parameter.pageNum
          this.pageSize = parameter.pageSize
          return res
        })
      },

      // 查询参数
      queryParam: { status: '' },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width: '80px'
        },
        {
          title: '图片',
          dataIndex: 'image',
          ellipsis: true,
          width: '250px',
          scopedSlots: { customRender: 'image' }
        },
        {
          title: '标题',
          dataIndex: 'title',
          ellipsis: true
        },
        {
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectedRowKeys: [],
      selectedRows: [],
      ids: [],
      dataList: []
    }
  },
  created() {},
  methods: {
    handleAdd() {
      var params = {
        title: '新增标题',
        type: 'add'
      }
      this.$refs.elegantForm.add(params)
    },

    handleSmallEdit(record) {
      var params = {
        title: '编辑标题',
        type: 'edit',
        data: record
      }
      this.$refs.elegantForm.edit(params)
    },

    handleOk() {
      this.$refs.table.refresh(true)
    },
    handleReload() {
      this.$refs.table.refresh()
    },
    delByIds(info) {
      var _this = this
      this.$confirm({
        title: `确定删除'${info.title}'？`,
        onOk() {
          var params = {
            id: info.id
          }
          elegantlDel(params).then(res => {
            _this.$message.success(`删除成功`)
            _this.handleOk()
          })
        },
        onCancel() {}
      })
    },

    onSelectChange(selectedRowKeys, selectedRows) {
      this.ids = []
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      for (var item of selectedRows) {
        this.ids.push(item.id)
      }
    }
  }
}
</script>
<style scoped lang="less"></style>
